package com.jyy.prefabricated.controller;


import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.TrainingOrganizationExamDto;
import com.jyy.prefabricated.entity.SysUser;
import com.jyy.prefabricated.service.DowloadService;
import com.jyy.prefabricated.service.TrainingOrganizationService;
import com.jyy.prefabricated.utils.UserUtils;
import com.jyy.prefabricated.vo.StudentExamScoreVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RequestMapping("/api")
@RestController
public class DowloadPaperController {
    @Autowired
    private DowloadService dowloadService;
    @Autowired
    private TrainingOrganizationService trainingOrganizationService;

    @Autowired
    private UserUtils userUtils;

    /**
     * 下载完整试卷
     *
     * @param id 试卷id
     * @return
     */
    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public AjaxResult dowloadPaper(Integer id, Integer paperType) throws IOException {
        String dowloadUrl = dowloadService.dowloadPaper(id, paperType);
        return AjaxResult.success(dowloadUrl);
    }


    /**
     * 导出全部学员考卷
     * examId 考试id
     *
     * @return
     */
    @RequestMapping(value = "/downloadScore", method = RequestMethod.GET)
    public AjaxResult dowloadScorePaper(Integer examId) {
        String dowloadUrl = dowloadService.exportExam(examId);
        return AjaxResult.success(dowloadUrl);
    }

    /**
     * 导出单个学员考卷
     *
     * @param studentExamId 学生考试id
     * @return
     */
    @RequestMapping(value = "/downloadPersonScore", method = RequestMethod.GET)
    public AjaxResult dowloadPersonScorePaper(Integer studentExamId) {

        String dowloadUrl = dowloadService.exportPersonExam(studentExamId);
        return AjaxResult.success(dowloadUrl);
    }


    /**
     * 企业导出学员考卷
     * examId 考试id
     *
     * @return
     */
    @RequestMapping(value = "/downloadExam", method = RequestMethod.GET)
    public AjaxResult dowloadExamPaper(TrainingOrganizationExamDto dto) {

        if (dto.getType() == 1) {
            SysUser user = userUtils.getCurrentUser();
            List<StudentExamScoreVo> studentExamVoList = trainingOrganizationService.selectExamRecord(dto.getExamId(), user.getTrainingOrganization().getId());
            if (studentExamVoList != null && studentExamVoList.size() > 0) {
                List<Integer> studentExamIdList = studentExamVoList.stream().map(e -> e.getStudentExamId()).collect(Collectors.toList());
                dto.setStudentExamIdList(studentExamIdList);
            }
        }
        String dowloadUrl = dowloadService.exportTrainingOrganizationExam(dto);
        return AjaxResult.success(dowloadUrl);
    }


    /**
     * 下载个人学员试卷主观题部分
     *
     * @param studentExamIdList 学生考试id
     * @return
     */
    @RequestMapping(value = "/downloadPersonSubjective", method = RequestMethod.GET)
    public AjaxResult dowloadPersonSubjective(@RequestParam List<Integer> studentExamIdList) {

        String dowloadUrl = dowloadService.exportPersonSubjectiveExam(studentExamIdList);
        return AjaxResult.success(dowloadUrl);
    }

    /**
     * 导出单个学员练习
     *
     * @param studentExerciseId 学生练习id
     * @return
     */
    @RequestMapping(value = "/downloadPersonExercise", method = RequestMethod.GET)
    public AjaxResult dowloadPersonExerciser(Integer studentExerciseId) {

        Object dowloadUrl = dowloadService.exportPersonExercise(studentExerciseId);
        return AjaxResult.success(dowloadUrl);
    }


}
